package mr.zhang.weixin.wechatutil;

import mr.zhang.weixin.pojo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 管理器
 * 
 * @author jerry
 *
 */
public class MenuManager {

	private static Logger log = LoggerFactory.getLogger(MenuManager.class);

	public static void main(String[] args) {
		// 第三方用户唯一凭证
		String appId = "wxe15bdd965e664afc";
		// 第三方用户唯一凭证密钥
		String appSecret = "dc399bfb6bfac8596076d17071a56a45";

		// 调用接口获取access_token
		AccessToken at = WeChatUtil.getAccessToken(appId, appSecret);

		if (null != at) {
			// 调用接口创建菜单
			int result = WeChatUtil.createMenu(getMenu(), at.getToken());

			// 判断菜单创建结果
			if (0 == result) {
				log.info("菜单创建成功！");

			}
			else
				log.info("菜单创建失败，错误码：" + result);
		}
	}

	/**
	 * 组装菜单数据
	 *
	 * @return
	 */
	public static Menu getMenu() {



		CommonButton btn11 = new CommonButton();
		btn11.setName("天气预报");
		btn11.setType("click");
		btn11.setKey("11");

		CommonButton btn12 = new CommonButton();
		btn12.setName("热门信息");
		btn12.setType("click");
		btn12.setKey("12");



		CommonButton btn13 = new CommonButton();
		btn13.setName("周边搜索");
		btn13.setType("click");
		btn13.setKey("13");

		CommonButton btn14 = new CommonButton();
		btn14.setName("历史上的今天");
		btn14.setType("click");
		btn14.setKey("14");

		CommonButton btn21 = new CommonButton();
		btn21.setName("西昌新闻");
		btn21.setType("click");
		btn21.setKey("21");

		CommonButton btn22 = new CommonButton();
		btn22.setName("商家促销");
		btn22.setType("click");
		btn22.setKey("22");

		CommonButton btn23 = new CommonButton();
		btn23.setName("抢优惠");
		btn23.setType("click");
		btn23.setKey("23");

		CommonButton btn24 = new CommonButton();
		btn24.setName("抢银行");
		btn24.setType("click");
		btn24.setKey("24");

		CommonButton btn25 = new CommonButton();
		btn25.setName("同城聊天");
		btn25.setType("click");
		btn25.setKey("25");

		ViewButton btn31 = new ViewButton();
		btn31.setName("进入官网");
		btn31.setType("view");
		btn31.setUrl("http://www.baidu.com/");

		CommonButton btn32 = new CommonButton();
		btn32.setName("商家合作圈");
		btn32.setType("click");
		btn32.setKey("32");

		CommonButton btn33 = new CommonButton();
		btn33.setName("投诉建议");
		btn33.setType("click");
		btn33.setKey("33");

		ComplexButton mainBtn1 = new ComplexButton();
		mainBtn1.setName("咨询");
		mainBtn1.setSub_button(new CommonButton[] { btn11, btn12, btn13, btn14 });

		ComplexButton mainBtn2 = new ComplexButton();
		mainBtn2.setName("服务");
		mainBtn2.setSub_button(new CommonButton[] { btn21, btn22, btn23, btn24,
				btn25 });

		ComplexButton mainBtn3 = new ComplexButton();
		mainBtn3.setName("交友");
		mainBtn3.setSub_button(new CommonButton[] { btn31, btn32, btn33 });


		Menu menu = new Menu();
		menu.setButton(new WeChatButton[] { mainBtn1, mainBtn2, mainBtn3 });

		log.info("success" + "创建菜单成功");
		System.out.println("创建菜单成功"+menu.getButton());

		return menu;
	}

}
